//
//  WHInputInfoCell.m
//  SquirrelSeller
//
//  Created by 邓伟浩 on 2019/1/13.
//  Copyright © 2019 邓伟浩. All rights reserved.
//

#import "WHInputInfoCell.h"

@implementation WHInputInfoCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self setUpAuthInfoUI];
    }
    return self;
}

- (void)setUpAuthInfoUI {
    [self.contentView addSubview:({
        _tipsLabel = [[UILabel alloc] init];
        _tipsLabel.textAlignment = NSTextAlignmentLeft;
        _tipsLabel.font = SYSTEMFONT(kRealwidth(16));
        _tipsLabel.textColor = CFontTitleBlackColor;
        _tipsLabel;
        
    })];
    
    [_tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView.mas_left).offset(kRealwidth(15));
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.height.mas_equalTo(kRealwidth(16));
    }];
    [_tipsLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
    [self.contentView addSubview:({
        _messageField = [[UITextField alloc] init];
        _messageField.textAlignment = NSTextAlignmentLeft;
        _messageField.font = SYSTEMFONT(kRealwidth(15));
        _messageField.textColor = CFontTitleBlackColor;
        
        _messageField;
        
    })];
    
    [_messageField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.contentView.mas_right).offset(-kRealwidth(50));
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.height.mas_equalTo(self.mas_height);
        make.left.mas_equalTo(_tipsLabel.mas_right).offset(kRealwidth(40));
    }];
    [_messageField setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
}

- (void)setInputTextPlaceHolder:(NSString *)placeHolderStr {
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:placeHolderStr attributes:@{NSForegroundColorAttributeName:CFontContentLightGrayColor, NSFontAttributeName:self.messageField.font}];
    self.messageField.attributedPlaceholder = attrString;
}

- (void)prepareForReuse {
    _messageField.text = @"";
    _tipsLabel.text = @"";
    
    [super prepareForReuse];
}

- (void)setValidType:(ValideType)validType {
    _validType = validType;
    
    kWeakSelf(self);
    if (validType == ValideTypeUserName) {
        self.messageField.keyboardType = UIKeyboardTypeDefault;
        self.messageField.yb_inputCP = YBInputControlProfile.creat.set_maxLength(10).set_textChanged(^(id obj) {
            if (weakself.textChangeBlock) {
                weakself.textChangeBlock([obj valueForKey:@"text"]);
            }
        });
    } else if (validType == ValideTypeIDCard) {
        self.messageField.keyboardType = UIKeyboardTypeASCIICapable;
        self.messageField.yb_inputCP = YBInputControlProfile.creat.set_maxLength(20).set_regularStr(@"^(([0-9]{0,18})|([0-9]{0,17}((x)|(X))))$").set_textChanged(^(id obj) {
            if (weakself.textChangeBlock) {
                weakself.textChangeBlock([obj valueForKey:@"text"]);
            }
        });
    } else if (validType == ValideTypePhoneNumber) {
        self.messageField.keyboardType = UIKeyboardTypeNumberPad;
        self.messageField.yb_inputCP = YBInputControlProfile.creat.set_maxLength(11).set_regularStr(@"^[0-9]{0,11}+$").set_textChanged(^(id obj) {
            if (weakself.textChangeBlock) {
                weakself.textChangeBlock([obj valueForKey:@"text"]);
            }
        });
    } else if (validType == ValideTypePayPassword) {
        self.messageField.keyboardType = UIKeyboardTypeNumberPad;
        self.messageField.yb_inputCP = YBInputControlProfile.creat.set_maxLength(11).set_regularStr(@"^[0-9]{0,6}+$").set_textChanged(^(id obj) {
            if (weakself.textChangeBlock) {
                weakself.textChangeBlock([obj valueForKey:@"text"]);
            }
        });
    } else if (validType == ValideTypeAmount) {
        self.messageField.keyboardType = UIKeyboardTypeDecimalPad;
        self.messageField.yb_inputCP = YBInputControlProfile.creat.set_maxLength(NSUIntegerMax).set_regularStr(@"^[0-9]{0,10}+(\\.([0-9]{0,2}))?$").set_textChanged(^(id obj) {
            if (weakself.textChangeBlock) {
                weakself.textChangeBlock([obj valueForKey:@"text"]);
            }
        });
    } else if (validType == ValideTypeBankCard) {
        self.messageField.keyboardType = UIKeyboardTypeNumberPad;
        self.messageField.yb_inputCP = YBInputControlProfile.creat.set_maxLength(19).set_regularStr(@"^[0-9]{0,19}+$").set_textChanged(^(id obj) {
            if (weakself.textChangeBlock) {
                weakself.textChangeBlock([obj valueForKey:@"text"]);
            }
        });
    } else if (validType == ValideTypePassword) {
        self.messageField.keyboardType = UIKeyboardTypeASCIICapable;
        self.messageField.yb_inputCP = YBInputControlProfile.creat.set_maxLength(12).set_regularStr(@"^[a-zA-Z0-9]{0,12}$").set_textChanged(^(id obj) {
            if (weakself.textChangeBlock) {
                weakself.textChangeBlock([obj valueForKey:@"text"]);
            }
        });
    } else {
        self.messageField.keyboardType = UIKeyboardTypeDefault;
        self.messageField.yb_inputCP = YBInputControlProfile.creat.set_maxLength(15).set_textChanged(^(id obj) {
            if (weakself.textChangeBlock) {
                weakself.textChangeBlock([obj valueForKey:@"text"]);
            }
        });
    }
}

- (void)dealloc {
    DLog(@"WHInputInfoCell");
}

@end
